# coding=utf-8

from resourcesservice import resourcesservice
from persistents import RedisServices
from datetime import datetime


class DevicesManagerServices(object):

    def __init__(self):
        self.resource = resourcesservice()
        self.rds_handler = RedisServices()
        self.prefix_devices = 'device_'

    def get_devices_info(self):
        result = []
        for device in self.rds_handler.keys(self.prefix_devices):
            deviceinfo = self.rds_handler.hgetall(device)
            deviceinfo['create_time'] = device.split('_')[-1]
            result.append(deviceinfo)
        return result

    def set_a_device_info(self, info):
        self.rds_handler.device_info_insert(self.prefix_devices + str(datetime.now()), info)
